<?php
namespace addons\auxiliary\controller;
use app\common\controller\Addons;
class Auxiliary extends Addons{

	protected $model = '';

	public function _initialize(){
		parent::_initialize();
		$this->model = new Auxiliary();
	}

	/**
	 * 赞、踩、收藏、与数据库沟通的方法
	 * @param type 类型
	 * @author colin <colin@tensent.cn>
	 */
	public function append($type = null){
		$map['type'] = $this->IS_NULL(input('get.type'),'参数传递错误！');
		$map['uid'] = $this->IS_NULL(session('user_auth.uid'));
		$map['aid'] = $this->IS_NULL(input('get.aid'),'请进入文章页后操作');
		$find = $this->$map['type']($map);
		$this->success($find ? ($find['number']+1) : 1);
		exit;
	}

	/**
	 * 点赞和踩操作
	 * @param map 数组
	 * @return null
	 * @author colin <colin@tensent.cn>
	 */
	protected function praisetread($map){
		$find = $this->model->where($map)->find();
		//是否超过时间
		$this->interval_time($find['start_time']);
		if($find){
			$this->model->where(array('id'=>$map['aid']))->setInc('number');
			$this->model->where(array('id'=>$map['aid']))->save(array('start_time'=>time()));
		}else{
			$data = $this->model->create();
			$this->model->add();
		}
		return $find;
	}

	/**
	 * 点赞操作
	 * @param map 数组
	 * @return null
	 * @author colin <colin@tensent.cn>
	 */
	public function praise($map){
		return $this->praisetread($map);
	}

	/**
	 * 踩操作
	 * @param map 数组
	 * @return null
	 * @author colin <colin@tensent.cn>
	 */
	public function tread($map){
		return $this->praisetread($map);
	}

	/**
	 * 收藏操作
	 * @param map 数组
	 * @return null
	 * @author colin <colin@tensent.cn>
	 */
	public function collection($map){
		unset($map['type']);
		$model = new AuxiliaryCollection();
		$find = $model->where($map)->find();
		//是否超过时间
		$this->interval_time($find['start_time']);
		if($find){
			//$model->where($map)->delete();
			$this->error('已收藏');
		}else{
			if(!session('__PREV__')){
				$this->error('参数错误！请重新进入此页面重试！');
			}
			$model->create();
			$model->add();
			$this->model->create();
			$this->model->add();
			$this->error('已收藏');
			exit;
		}
		
	}

	/**
	 * 是否超过时间
	 * @param start_time 开始时间
	 * @return null
	 * @author colin <colin@tensent.cn>
	 */
	public function interval_time($start_time){
		$config = new AuxiliaryAddon();
		$config = $config->Config();
		if((time() - $start_time) < $config['interval_time']){
			$this->error($config['tip_message']);
			exit;
		}
	}

	/**
	 * 查询number
	 * @author colin <colin@tensent.cn>
	 */
	public function getNumber(){
		$map['type'] = $this->IS_NULL(I('get.type'),'参数传递错误！');
		$map['aid'] = $this->IS_NULL(I('get.aid'),'请进入文章页后操作');
		$find = $this->model->where($map)->field('number')->find();
		if(!$find){
			$find['number'] = 0;
		}
		$this->success($find['number']);
	}

	/**
	 * 验证是否为空
	 * @param field 字段
	 * @param message 提示信息
	 * @return string
	 * @author colin <colin@tensent.cn>
	 */
	protected function IS_NULL($field,$message = '请登录后操作'){
		if(empty($field)){
			$this->error($message);
			exit;
		}
		return $field;
	}
}